#include <iostream>
#include "Epix3D.h"

using namespace std;
using namespace epix;

typedef TMesh<DVBO> Mesh;
typedef TImage<DVBO> Image;

int main(int argc, char **argv)
{
	int width = 1024, height = 768;
	bool fullscreen = false;

	if(argc >= 3)
    {
      istringstream iss1(argv[1]);
      iss1 >> width;
      istringstream iss2(argv[2]);
      iss2 >> height;
      if(argc >= 4)
		fullscreen = true;
    }

	Window *Window = Window::instance();
	Renderer *rdr = Window->getRenderer();
	EventManager *emgr = Window->getEventManager();

	Window->init(width, height, fullscreen);

	rdr->initExtensions();

	///Scene
	Mesh *scene = new Mesh("media/scene.obj");
	scene->setTexture("textures/rock.tga");
	scene->setScale(Vector3d(3, 3, 3));

	Mesh *scene2 = new Mesh("media/monkey.obj");
	scene2->setTexture("textures/suzanne.bmp");
	scene2->setPosition(Vector3d(0, 0, 4));

	FreeFlyCamera *cam = new FreeFlyCamera(Vector3d(1, 5, 5));
	cam->setSensivity(1.0);
	cam->setSpeed(5.0);

	Shader *light = new Shader("shaders/light.vert", "shaders/light.frag");
	Shader *cartoon = new Shader("shaders/celshading.vert", "shaders/celshading.frag");
	Shader *shader = NULL;

	Image *sceneT = new Image("textures/suzanne.bmp", 0, Window->getSize().Y-100);
	sceneT->setSize(100, 100);
	Image *scene2T = new Image("textures/rock.tga", sceneT->getSize().X, Window->getSize().Y-100);
	scene2T->setSize(100, 100);

	Skybox *skybox = new Skybox("textures/back.jpg", "textures/front.jpg",
								"textures/bottom.jpg", "textures/top.jpg",
								"textures/right.jpg", "textures/left.jpg");
	skybox->setSize(10.0f);

	Window->showCursor(false);
 
	while(Window->run())
    {
		Window->setWindowTitle("Blue3D : test des VBOs, OpenGL", true);
		emgr->getEvent();
		cam->onEvent();
		cam->animate();
		skybox->setPosition(cam->getPosition());

		rdr->beginScene();
		cam->look();
		skybox->draw();
		scene->draw();
		scene2->draw();
 
		if(shader) shader->setShader(false);

		rdr->set2DMode();
		sceneT->draw();
		scene2T->draw();
		rdr->end2DMode();

		if(shader) shader->setShader(true);

		rdr->endScene();

		if(emgr->quit()) Window->closeWindow();
		else if(emgr->isKeyDownOnce(GLFW_KEY_ESC)) Window->closeWindow();
		else if(emgr->isKeyDownOnce('A')) Window->showCursor(false);
		else if(emgr->isKeyDownOnce('E')) Window->showCursor(true);
		else if(emgr->isKeyDownOnce('I'))
        {
			cartoon->setShader(false);
			light->setShader(false);
			shader = NULL;
        }
		else if(emgr->isKeyDownOnce('O'))
        {
			cartoon->setShader(false);
			light->setShader(true);
			shader = light;
        }
		else if(emgr->isKeyDownOnce('P'))
        {
			light->setShader(false);
			cartoon->setShader(true);
			shader = cartoon;
        }
		else if(emgr->isKeyDownOnce('K'))
			rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
		else if(emgr->isKeyDownOnce('L'))
			rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		else if(emgr->isKeyDownOnce('M'))
			rdr->setPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    }

	delete cam;
	delete skybox;
	delete sceneT;
	delete scene2T;
	delete scene;
	delete scene2;
	delete cartoon;
	delete light;
	
	Window::destroy();

	return 0;
}

